import DatePicker from 'vue-datepicker'
import Tab from '../tab/tab.vue'
import { showMsg, setMenuType, updateBanner, getBanner } from '../../../../vuex/action'
import getDatePickerOption from '../../../../util/date-picker-option'
import { getDate } from '../../../../util/formatDate'

const validation = (data) => {
  for (let name in data) {
    if ((data[name] + '').length === 0) {
      return '* 标记的均为必填/必选项'
    }
    if (data.startTime >= data.endTime) {
      return '开始时间需早于结束时间'
    }
  }
  return true
}

export default {
  name: 'promoter-list',
  components: {
    'vue-tab': Tab,
    'vue-date-picker': DatePicker
  },
  data() {
    return {
      startTime: '',
      endTime: '',
      desc: '',
      datePickerOption: getDatePickerOption()
    }
  },
  computed: {
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xSetMenuType: setMenuType,
      xUpdateBanner: updateBanner,
      xGetBanner: getBanner
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getMsgInfo()
  },
  methods: {
    submit() {
      var params = {
        startTime: this.startTime,
        endTime: this.endTime,
        desc: this.desc,
      }
      var valid = validation(params)
      if (valid !== true) {
        return this.xShowMsg({
          type: 'danger',
          content: valid
        })
      }
      var formData = new FormData()
      formData.append('startTime', new Date(this.startTime))
      formData.append('endTime', new Date(this.endTime))
      formData.append('desc', this.desc)
      this.xUpdateBanner(formData)
        .then((res) => {
          this.xShowMsg({
            type: 'info',
            content: '更新成功'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    getMsgInfo() {
      this.xGetBanner()
        .then((res) => {
          this.startTime = getDate(res.msg.startTime)
          this.endTime = getDate(res.msg.endTime)
          this.desc = res.msg.desc
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
